package ie.wit.loanmanager.gui;

import ie.wit.loanmanager.ControllerSystem;
import ie.wit.loanmanager.exceptions.TheLoanListIsEmptyException;
import ie.wit.loanmanager.exceptions.ThereIsNoLoanWithThisIdException;
import ie.wit.loanmanager.exceptions.UnknownItemException;
import ie.wit.loanmanager.model.Loan;
import ie.wit.loanmanager.utils.Validate;

import java.net.URL;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class LoanManagerGui {
	
		private static Icon myIcon = getImageResource("/images/lendborrow.png");
		private static Icon anIcon = new ImageIcon();
		private static int currentSize = 0, maxSize = 14;
		
		private Loan[] DeviceList = new Loan[maxSize];

		private static ImageIcon getImageResource(String path) {
			
			URL url = LoanManagerGui.class.getResource(path);
			return new ImageIcon(url);
		}
		////////////////////////////////////////////////////////////////////////////////////////	
	 
		public int menuMain()
		{
		int option = 0;
		
		   String opt1 = new String("1. Add a new loan :");
		   String opt2 = new String("2. Remove a loan by Id :");
		   String opt3 = new String("3. List All Loans :");
		   String opt4 = new String("4. List All Loans with ME :");
		   String opt5 = new String("5. Exit: ");
		   String msg = new String("Enter Option:");
		   JTextField opt = new JTextField("");
		   
		   Object message[] = new Object[9];

		   message[0] = myIcon;
		   message[1] = opt1;
		   message[2] = opt2;
		   message[3] = opt3;
		   message[4] = opt4;
		   message[5] = opt5;
		   message[6] = msg;
		   message[7] = opt;

		   int response = JOptionPane.showConfirmDialog(null,message,"Device Data Entry",JOptionPane.OK_CANCEL_OPTION,
				                                        JOptionPane.QUESTION_MESSAGE ,anIcon);
		   
		   if(response == JOptionPane.CANCEL_OPTION)
			   ;
		   else
		   	{
			   try {
				   option = Integer.parseInt( opt.getText());
			   	   }
			   catch (Exception e)
			   {
			   JOptionPane.showMessageDialog(null,"Data Input Error" + e + "\nPlease Try Again");
			   }
		   	}
		   return option;
		}
		
	
	public Object menuAddLoan(String v1, String v2, String v3,
							  String v4, String v5, String v6
							  ) throws NumberFormatException, UnknownItemException
	{
		
		   String msgName = new String("Who got it? (Enter 'ME' if you've borrowed the item");
		   String msgMail = new String("Email :");
		   String msgPhone = new String("Phone Number :");
		   String msgDate = new String("Date to be returned. [Please, enter date in the following format: dd-MM-YY]");
		   String msgDescription = new String("Loan description :");
		   String msgChooseType = new String("Choose a type: ");
		   String[] typesAvailable = {"Select a option...", "CD", "DVD", "Money", "Other"};
		   
		   JTextField name = new JTextField(v1);
		   JTextField email = new JTextField(v2);
		   JTextField phone = new JTextField(v3);
		   JTextField description = new JTextField(v4);
		   JComboBox type = new JComboBox(typesAvailable);
		   JTextField date = new JTextField(v5);
		   
		   Object message[] = new Object[14];
	
		   message[0] = myIcon;
		   message[1] = msgName;
		   message[2] = name;
		   message[3] = msgMail;
		   message[4] = email;
		   message[5] = msgPhone;
		   message[6] = phone;
		   message[7] = msgDate;
		   message[8] = date;
		   message[9] = msgDescription;
		   message[10] = description;
		   message[11] = msgChooseType;
		   message[12] = type;
		   
		   
		   int response = JOptionPane.showConfirmDialog(null,message,"Device Data Entry",JOptionPane.OK_CANCEL_OPTION,
				                                        JOptionPane.QUESTION_MESSAGE ,anIcon);
		   
		   if(response == JOptionPane.CANCEL_OPTION)
			   ;
		   else
		   	{
			   
			   if (name.getText().trim().equalsIgnoreCase("")) {
				   JOptionPane.showMessageDialog(null,"Please, enter the field name");
				   return menuAddLoan(name.getText(), email.getText(), phone.getText(),
						   description.getText(), date.getText(), type.getSelectedItem().toString());
				   
			   }
			   
			   if (!Validate.isValidEmailAddress(email.getText())) {
				   JOptionPane.showMessageDialog(null,"Invalid email address.");
				   return menuAddLoan(name.getText(), email.getText(), phone.getText(),
						   description.getText(), date.getText(), type.getSelectedItem().toString());
			   }
			   
			   
			   if (!Validate.isValidDate(date.getText())) {
				   JOptionPane.showMessageDialog(null,"Invalid date format.");
				   return menuAddLoan(name.getText(), email.getText(), phone.getText(),
						   description.getText(), date.getText(), type.getSelectedItem().toString());
			   }
			   
			   int year = 0 ;
			   int month = 0;
			   int day = 0;
			   
			   try {
				   year = Integer.parseInt(date.getText().split("-")[2]);
				   month = Integer.parseInt(date.getText().split("-")[1]);
				   day = Integer.parseInt(date.getText().split("-")[0]);

			   } catch (Exception e) {
				   JOptionPane.showMessageDialog(null,"Invalid date format.");
				   return menuAddLoan(name.getText(), email.getText(), phone.getText(),
						   description.getText(), date.getText(), type.getSelectedItem().toString());
			   }
			   
			   
			   Calendar c = Calendar.getInstance();
			   if (c.compareTo(new GregorianCalendar(year, month, day)) > 0) {
				   JOptionPane.showMessageDialog(null,"Return data before actual date.");
				   return menuAddLoan(name.getText(), email.getText(), phone.getText(),
						   description.getText(), date.getText(), type.getSelectedItem().toString());
			   }
			   

			   String selectedItem = type.getSelectedItem().toString();
			   if (selectedItem.equalsIgnoreCase("select a option...")) {
				   JOptionPane.showMessageDialog(null,"Please, select the item's type...");
				   return menuAddLoan(name.getText(), email.getText(), phone.getText(),
						   description.getText(), date.getText(), type.getSelectedItem().toString());
			   }
			   
			   ControllerSystem.getInstance().addNewLoan(name.getText().trim(), email.getText(), phone.getText(),
					   						year, month, day,
					   						description.getText(), selectedItem);
			   
			   JOptionPane.showMessageDialog(null,"Loan recorded successfully");
		   	}
		   
		   return null;
	}
	
	
	
	
	
	public void menuRemoveLoan()
	{
		   String msgId = new String("Please, enter the id you'd like to delete");
		   		   
		   JTextField id = new JTextField("");
		   
		   Object message[] = new Object[10];
	
		   message[0] = myIcon;
		   message[1] = msgId;
		   message[2] = id;
		   
		   
		   int response = JOptionPane.showConfirmDialog(null,message,"Device Data Entry",JOptionPane.OK_CANCEL_OPTION,
				                                        JOptionPane.QUESTION_MESSAGE ,anIcon);
		   
		   if(response == JOptionPane.CANCEL_OPTION)
			   ;
		   else
		   	{
			   ControllerSystem c = ControllerSystem.getInstance();
			   try {
				c.removeLoanById(Integer.parseInt(id.getText()));
				JOptionPane.showMessageDialog(null,"Loan deleted successfully.");
			   } catch (NumberFormatException e) {
				   JOptionPane.showMessageDialog(null,"Number Format Exception");
				   
			   } catch (ThereIsNoLoanWithThisIdException e) {
				   JOptionPane.showMessageDialog(null,"There Is No Loan With This Id Exception");
				
			   } catch (TheLoanListIsEmptyException e) {
				   JOptionPane.showMessageDialog(null,"The Loan List Is Empty");
				
			   }
		   	}
			   
			   
	}
	
	

	}
